package com.dianping.cat.consumer.business.model.transform;

import com.dianping.cat.consumer.business.model.entity.BusinessItem;
import com.dianping.cat.consumer.business.model.entity.BusinessReport;
import com.dianping.cat.consumer.business.model.entity.Segment;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;







public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public BusinessItem buildBusinessItem(Attributes attributes) {
/*  22 */     String id = attributes.getValue("id");
/*  23 */     String type = attributes.getValue("type");
    BusinessItem businessItem = new BusinessItem(id);

/*  26 */     if (type != null) {
/*  27 */       businessItem.setType(type);
    }

/*  30 */     return businessItem;
  }


  public BusinessReport buildBusinessReport(Attributes attributes) {
/*  35 */     String domain = attributes.getValue("domain");
    String startTime = attributes.getValue("startTime");
/*  37 */     String endTime = attributes.getValue("endTime");
/*  38 */     BusinessReport businessReport = new BusinessReport(domain);

/*  40 */     if (startTime != null) {
/*  41 */       businessReport.setStartTime(toDate(startTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  44 */     if (endTime != null) {
/*  45 */       businessReport.setEndTime(toDate(endTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  48 */     return businessReport;
  }


  public Segment buildSegment(Attributes attributes) {
/*  53 */     String id = attributes.getValue("id");
/*  54 */     String count = attributes.getValue("count");
/*  55 */     String sum = attributes.getValue("sum");
/*  56 */     String avg = attributes.getValue("avg");
/*  57 */     Segment segment = new Segment((id == null) ? null : (Integer)convert(Integer.class, id, null));

/*  59 */     if (count != null) {
/*  60 */       segment.setCount(((Integer)convert(Integer.class, count, Integer.valueOf(0))).intValue());
    }

/*  63 */     if (sum != null) {
/*  64 */       segment.setSum(((Double)convert(Double.class, sum, Double.valueOf(0.0D))).doubleValue());
    }

/*  67 */     if (avg != null) {
/*  68 */       segment.setAvg(((Double)convert(Double.class, avg, Double.valueOf(0.0D))).doubleValue());
    }

/*  71 */     return segment;
  }


  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/*  76 */     if (value == null || value.length() == 0) {
/*  77 */       return defaultValue;
    }

/*  80 */     if (type == Boolean.class || type == boolean.class)
/*  81 */       return (T)Boolean.valueOf(value); 
/*  82 */     if (type == Integer.class || type == int.class)
/*  83 */       return (T)Integer.valueOf(value); 
/*  84 */     if (type == Long.class || type == long.class)
/*  85 */       return (T)Long.valueOf(value); 
/*  86 */     if (type == Short.class || type == short.class)
/*  87 */       return (T)Short.valueOf(value); 
/*  88 */     if (type == Float.class || type == float.class)
/*  89 */       return (T)Float.valueOf(value); 
/*  90 */     if (type == Double.class || type == double.class)
/*  91 */       return (T)Double.valueOf(value); 
/*  92 */     if (type == Byte.class || type == byte.class)
/*  93 */       return (T)Byte.valueOf(value); 
/*  94 */     if (type == Character.class || type == char.class) {
/*  95 */       return (T)Character.valueOf(value.charAt(0));
    }
/*  97 */     return (T)value;
  }


  protected Date toDate(String str, String format, Date defaultValue) {
/* 102 */     if (str == null || str.length() == 0) {
/* 103 */       return defaultValue;
    }

    try {
/* 107 */       return (new SimpleDateFormat(format)).parse(str);
/* 108 */     } catch (ParseException e) {
/* 109 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/business/model/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */